<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>用netstat查看网络状态详解 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>用netstat查看网络状态详解</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux/">Linux</a>
			</span>
		
	</div>

	

	
		<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E7%94%A8netstat%E6%9F%A5%E7%9C%8B%E7%BD%91%E7%BB%9C%E7%8A%B6%E6%80%81%E8%AF%A6%E8%A7%A3/01.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E7%94%A8netstat%E6%9F%A5%E7%9C%8B%E7%BD%91%E7%BB%9C%E7%8A%B6%E6%80%81%E8%AF%A6%E8%A7%A3/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;通常情况下:一个正常的TCP连接，都会有三个阶段:</p>
<ol>
<li>TCP三次握手;</li>
<li>数据传送;</li>
<li>TCP四次挥手</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注:以下说明最好能结合”图:TCP的状态机”来理解。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>SYN</strong>: (同步序列编号,Synchronize Sequence Numbers)该标志仅在三次握手建立TCP连接时有效。表示一个新的TCP连接请求。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>ACK</strong>: (确认编号,Acknowledgement Number)是对TCP请求的确认标志,同时提示对端系统已经成功接收所有数据。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>FIN</strong>: (结束标志,FINish)用来结束一个TCP回话.但对应端口仍处于开放状态,准备接收后续数据。</p>
<ol>
<li><p>LISTEN:首先服务端需要打开一个socket进行监听，状态为LISTEN. /<em> The socket is listening for incoming connections. 侦听来自远方TCP端口的连接请求 </em>/</p>
</li>
<li><p>SYN_SENT:客户端通过应用程序调用connect进行active open.于是客户端tcp发送一个SYN以请求建立一个连接.之后状态置为SYN_SENT. <code>/*The socket is actively attempting to establish a connection. 在发送连接请求后等待匹配的连接请求 */</code></p>
</li>
<li><p>SYN_RECV:服务端应发出ACK确认客户端的SYN,同时自己向客户端发送一个SYN. 之后状态置为SYN_RECV <code>/* A connection request has been received from the network. 在收到和发送一个连接请求后等待对连接请求的确认 */</code></p>
</li>
<li><p>ESTABLISHED: 代表一个打开的连接，双方可以进行或已经在数据交互了。<code>/* The socket has an established connection. 代表一个打开的连接，数据可以传送给用户 */</code></p>
</li>
<li><p>FIN_WAIT1:主动关闭(active close)端应用程序调用close，于是其TCP发出FIN请求主动关闭连接，之后进入FIN_WAIT1状态.<code>/* The socket is closed, and the connection is shutting down. 等待远程TCP的连接中断请求，或先前的连接中断请求的确认 */</code></p>
</li>
<li><p>CLOSE_WAIT:被动关闭(passive close)端TCP接到FIN后，就发出ACK以回应FIN请求(它的接收也作为文件结束符传递给上层应用程序),并进入CLOSE_WAIT. <code>/* The remote end has shut down, waiting for the socket to close. 等待从本地用户发来的连接中断请求 */</code></p>
</li>
<li><p>FIN_WAIT2:主动关闭端接到ACK后，就进入了FIN-WAIT-2 .<code>/* Connection is closed, and the socket is waiting for a shutdown from the remote end. 从远程TCP等待连接中断请求 */</code></p>
</li>
<li><p>LAST_ACK:被动关闭端一段时间后，接收到文件结束符的应用程序将调用CLOSE关闭连接。这导致它的TCP也发送一个 FIN,等待对方的ACK.就进入了LAST-ACK . <code>/* The remote end has shut down, and the socket is closed. Waiting for acknowledgement. 等待原来发向远程TCP的连接中断请求的确认 */</code></p>
</li>
<li><p>TIME_WAIT:在主动关闭端接收到FIN后，TCP就发送ACK包，并进入TIME-WAIT状态。<code>/* The socket is waiting after close to handle packets still in the network.等待足够的时间以确保远程TCP接收到连接中断请求的确认 */</code></p>
</li>
<li><p>CLOSING: 比较少见.<code>/* Both sockets are shut down but we still don’t have all our data sent. 等待远程TCP对连接中断的确认 */</code></p>
</li>
<li><p>CLOSED: 被动关闭端在接受到ACK包后，就进入了closed的状态。连接结束.<code>/* The socket is not being used. 没有任何连接状态 */</code></p>
</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;TIME_WAIT状态的形成只发生在主动关闭连接的一方。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主动关闭方在接收到被动关闭方的FIN请求后，发送成功给对方一个ACK后,将自己的状态由FIN_WAIT2修改为TIME_WAIT，而必须再等2倍 的MSL(Maximum Segment Lifetime,MSL是一个数据报在internetwork中能存在的时间)时间之后双方才能把状态 都改为CLOSED以关闭连接。目前RHEL里保持TIME_WAIT状态的时间为60秒。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当然上述很多TCP状态在系统里都有对应的解释或设置,可见man tcp</p>
<h2 id="关于长连接和短连接"><a href="#关于长连接和短连接" class="headerlink" title="关于长连接和短连接:"></a>关于长连接和短连接:</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;通俗点讲:短连接就是一次TCP请求得到结果后,连接马上结束.而长连接并不马上断开,而一直保持着,直到长连接TIMEOUT(具体程序都有相关参数说明).长连接可以避免不断的进行TCP三次握手和四次挥手.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;长连接(keepalive)是需要靠双方不断的发送探测包来维持的,keepalive期间服务端和客户端的TCP连接状态是ESTABLISHED.目前http 1.1版本里默认都是keepalive(1.0版本默认是不keepalive的)，ie6/7/8和firefox都默认用的是http 1.1版本了(如何查看当前浏览器用的是哪个版本，这里不再赘述)。Apache,java</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个应用至于到底是该使用短连接还是长连接，应该视具体情况而定。一般的应用应该使用长连接。</p>
<h3 id="Linux的相关keepalive参数"><a href="#Linux的相关keepalive参数" class="headerlink" title="Linux的相关keepalive参数"></a>Linux的相关keepalive参数</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">a、 tcp_keepalive_time - INTEGER</div><div class="line">How often TCP sends out keepalive messages when keepalive is enabled.</div><div class="line">Default: 2hours.</div><div class="line">b、 tcp_keepalive_probes - INTEGER</div><div class="line">How many keepalive probes TCP sends out, until it decides that the</div><div class="line">connection is broken. Default value: 9.</div><div class="line">c、 tcp_keepalive_intvl - INTEGER</div><div class="line">How frequently the probes are send out. Multiplied by</div><div class="line">tcp_keepalive_probes it is time to <span class="built_in">kill</span> not responding connection,</div><div class="line">after probes started. Default value: 75sec i.e. connection</div><div class="line">will be aborted after ~11 minutes of retries.</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E7%94%A8netstat%E6%9F%A5%E7%9C%8B%E7%BD%91%E7%BB%9C%E7%8A%B6%E6%80%81%E8%AF%A6%E8%A7%A3/03.png?raw=true" alt=""></figure></p>
<h3 id="F5负载均衡上的相关参数说明"><a href="#F5负载均衡上的相关参数说明" class="headerlink" title="F5负载均衡上的相关参数说明"></a>F5负载均衡上的相关参数说明</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">a、Keep Alive Interval</div><div class="line">Specifies, when enabled, how frequently the system sends data over an idle TCP connection, to determine whether the connection is still valid.</div><div class="line">Specify: Specifies the interval at <span class="built_in">which</span> the system sends data over an idle connection, to determine whether the connection is still valid. The default is 1800 milliseconds.</div><div class="line">b、Time Wait</div><div class="line">Specifies the length of time that a TCP connection remains <span class="keyword">in</span> the TIME-WAIT state before entering the CLOSED state.</div><div class="line">Specify: Specifies the number of milliseconds that a TCP connection can remain <span class="keyword">in</span> the TIME-WAIT state. The default is 2000.</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E7%94%A8netstat%E6%9F%A5%E7%9C%8B%E7%BD%91%E7%BB%9C%E7%8A%B6%E6%80%81%E8%AF%A6%E8%A7%A3/04.png?raw=true" alt=""></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">c、Idle Timeout</div><div class="line">Specifies the length of time that a connection is idle (has no traffic) before the connection is eligible <span class="keyword">for</span> deletion.</div><div class="line">Specify: Specifies a number of seconds that the TCP connection can remain idle before the system deletes it. The default is 300 seconds.</div></pre></td></tr></table></figure>
<h3 id="Apache的相关参数说明"><a href="#Apache的相关参数说明" class="headerlink" title="Apache的相关参数说明"></a>Apache的相关参数说明</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下是Apache/2.0.61版本的默认参数和说明</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">a、KeepAlive:</div><div class="line">default On.Whether or not to allow persistent connections (more than</div><div class="line">one request per connection). Set to “Off” to deactivate.</div><div class="line">b、MaxKeepAliveRequests:</div><div class="line">default 100.The maximum number of requests to allow</div><div class="line">during a persistent connection. Set to 0 to allow an unlimited amount.</div><div class="line">We recommend you leave this number high, <span class="keyword">for</span> maximum performance.</div><div class="line">c、KeepAliveTimeout:</div><div class="line">default 15. Number of seconds to <span class="built_in">wait</span> <span class="keyword">for</span> the next request from the</div><div class="line">same client on the same connection.</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E7%94%A8netstat%E6%9F%A5%E7%9C%8B%E7%BD%91%E7%BB%9C%E7%8A%B6%E6%80%81%E8%AF%A6%E8%A7%A3/05.png?raw=true" alt=""></figure></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/1. Linux 基础/57. 用netstat查看网络状态详解/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
